package com.itheima.user.controller;

import com.itheima.feign.order.OrderFeign;
import com.itheima.sh.entity.OrderEntity;
import com.itheima.sh.entity.UserEntity;
import com.itheima.sh.vo.request.AddOrderVO;
import com.itheima.sh.vo.response.OrderResultVO;
import com.itheima.sh.vo.response.ResultVO;
import com.itheima.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@RefreshScope//开启分布式配置中心动态刷新
public class UserController {

    @Autowired
    private UserService userService;

    //用户登录
    @GetMapping("/login/{userName}/{password}")
    public ResultVO login(@PathVariable("userName") String userName, @PathVariable("password") String password) {
        boolean loginResult = userService.login(userName, password);
        ResultVO result = new ResultVO();
        if (loginResult) {
            result.setSuccess(true);
        } else {
            result.setSuccess(false);
        }
        return result;
    }

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private OrderFeign orderFeign;

    //查询用户订单信息
    @GetMapping("/findUserOrders/{userId}")
    public OrderResultVO findUserOrders(@PathVariable("userId") Integer userId) {

        OrderResultVO orderResultVO = new OrderResultVO();
        UserEntity userEntity = userService.findUserById(userId);

        //todo
        //原始路径
//        String url = "http://localhost:9001/order/findOrderInfoByUserId/"+userId;


//        动态获取服务路径 euraka/Nacos  DiscoveryClient
//        List<ServiceInstance> instancesList = discoveryClient.getInstances("order-service");
//        if (instancesList == null || instancesList.size() == 0) {
//            return null;
//        }
//        ServiceInstance serviceInstance = instancesList.get(0);
//        String host = serviceInstance.getHost();
//        int port = serviceInstance.getPort();
//        String url = "http://"+host+":"+port+"/order/findOrderInfoByUserId/" + userId;

        //直接基于服务名称完成调用 Nacos中的Ribbon
//        String url = "http://order-service/order/findOrderInfoByUserId/" + userId;
//        List<OrderEntity> orderList = restTemplate.getForObject(url, List.class);

        //基于feign完成远程调用
        List<OrderEntity> orderList = orderFeign.findOrderInfoByUserId(userId);
        orderResultVO.setUserName(userEntity.getUserName());
        orderResultVO.setOrderList(orderList);
        return orderResultVO;
    }

    //添加订单
    @PostMapping("/addUserOrder")
    public ResultVO addUserOrder(@RequestBody AddOrderVO addOrderVO) {
        ResultVO result = new ResultVO();
        //判断用户是否存在
        UserEntity userEntity = userService.findUserById(addOrderVO.getUserId());
        if (userEntity == null) {
            result.setSuccess(false);
            return result;
        }

        //todo

        return result;
    }
    @RequestMapping("/hello")
    public String hello(String company){
        return "hello user service,company : "+company;
    }


    //资源3
    @GetMapping(value = "/hello3")
    public String hello3() {
        userService.dosomething();
        return "Hello Sentinel3";
    }

    //资源4
    @GetMapping(value = "/hello4")
    public String hello4() {
        userService.dosomething();
        return "Hello Sentinel4";
    }
    //资源5
    @GetMapping(value = "/hello5/{flag}")
    public String hello5(@PathVariable("flag") int flag) throws InterruptedException {
        if (flag == 1){
            TimeUnit.SECONDS.sleep(3);
        }
        return "Hello Sentinel5";
    }
}

